package org.strongme.modules.project.web;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.persistence.Page;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.Fy;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.SubProject;
import org.strongme.modules.project.entity.Tz;
import org.strongme.modules.project.service.FyService;
import org.strongme.modules.project.service.ProjectService;
import org.strongme.modules.project.service.TzService;
import org.strongme.modules.sys.utils.UserUtils;
import org.strongme.util.Encodes;
import org.strongme.util.StringUtils;

@Controller
@RequestMapping("project/fy")
public class FyController extends BaseController{
	
	@Autowired
	private FyService fyService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private TzService tzService;
	
	@ModelAttribute("fy")
	public Fy get(@RequestParam(required=false) String id, @RequestParam(required=false)String projectId, @RequestParam(required=false)String subProjectId,@RequestParam(required=false) String suoxiePro, @RequestParam(required=false) String suoxieSub) {
		Fy fy = null;
		if (StringUtils.isNotBlank(id)){
			fy = fyService.get(new Fy(id));
			if(projectId!=null&&!"".equals(projectId)&&fy!=null) {
				fy.setProject(new Project(projectId));
			}
		}else if(StringUtils.isNoneBlank(subProjectId)) {
			fy = new Fy();
			SubProject sp = new SubProject(subProjectId);
			fy.setSubProject(sp);
		}else if(StringUtils.isNoneBlank(projectId)) {
			fy = new Fy();
			fy.setProject(new Project(projectId));
		}else{
			fy = new Fy();
		}
		return fy;
	}
	
	@RequestMapping(value = { "index", "" })
	public String Index() {
		return "modules/project/fy/fyIndex.jsp";
	}
	
	@RequestMapping(value = {"list"})
	public String list(Fy fy, HttpServletRequest request, HttpServletResponse response, Model model) {
		String gg= fy.getGg();
		if(gg!=null) {
			gg = gg.replaceAll("&Icirc;&brvbar;", "Φ");
			gg = gg.replaceAll("&Atilde;", "×");
			fy.setGg(gg);
		}

		Page<Fy> page = fyService.findPage(new Page<Fy>(request, response), fy); 
        model.addAttribute("page", page);
        model.addAttribute("fy", fy);
        model.addAttribute("projectList", projectService.findList(new Project()));
		return "modules/project/fy/fyList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(Fy fy, Model model) {
		if(fy.getNotnew()!=null && "yes".equals(fy.getNotnew())) {
			fy.setId(null);
		}
		model.addAttribute("projectList", projectService.findList(new Project()));
		model.addAttribute("fy", fy);
		return "modules/project/fy/fyForm.jsp";
	}
	
	@RequestMapping(value = "save" ,method=RequestMethod.POST)
	public String save(Fy fy, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, fy)){
			return form(fy, model);
		}
		if(fy.getId()==null||"".equals(fy.getId())) {
			Integer count = fyService.existData(fy);
			if(count>0) {
				//已经存在此管线号,继续添加单条
				fyService.save(fy);
				addMessage(redirectAttributes, "保存副页数据成功");
			}else {
				//查询片数
				String gxh = fy.getJjbh();
				Tz tztmp = new Tz();tztmp.setGxh(gxh);SubProject sp = fy.getSubProject();sp.setProject(fy.getProject());
				tztmp.setSubProject(sp);
				List<Tz> tzList = tzService.findListByGXH(tztmp);
				if(tzList!=null&&tzList.size()>0) {
					for(Tz single : tzList) {
						Integer ps = Integer.valueOf(single.getPs());
						for(int i=0;i<ps;i++) {
							Fy fytmp = new Fy();
							fytmp.setProject(fy.getProject());
							fytmp.setSubProject(fy.getSubProject());
							fytmp.setJjmc(single.getJjmc());
							fytmp.setJjbh(single.getGxh());
							fytmp.setHkbh(single.getHkbh());
							fytmp.setPh(String.valueOf(i+1));
							fytmp.setGg(single.getGg());
							fytmp.setHgh(single.getHgh());
							fyService.save(fytmp);
						}
					}
					addMessage(redirectAttributes, "保存副页数据成功");
				}else {
					addMessage(redirectAttributes, "无相应焊口数据");
				}
			}
		}else {
			fyService.save(fy);
			addMessage(redirectAttributes, "保存副页数据成功");
		}
		String gg = fy.getGg();
//		%CE%A6%202%C3%972
		gg = gg.replaceAll("Φ", "%CE%A6");
		gg = gg.replaceAll(" ", "%20");
		gg = gg.replaceAll("×", "%C3%97");
		String returnStr = "redirect:/project/fy/list?project.id="+fy.getProject().getId()+"&subProject.id="+fy.getSubProject().getId()+"&jjbh="+fy.getJjbh()+"&gg="+gg+"&hkbh="+fy.getHkbh(); 
		return returnStr;
	}
	
	@RequestMapping(value = "delete" ,method=RequestMethod.GET)
	public String delete(Fy fy, Model model, RedirectAttributes redirectAttributes) {
		fyService.delete(fy);
		addMessage(redirectAttributes, "删除副页数据成功");
		return "redirect:/project/fy/list?project.id="+fy.getProject().getId()+"&subProject.id="+fy.getSubProject().getId();
	}

	@RequestMapping("toBatchForm")
	public String toBatchForm(String type, Model model) {
		model.addAttribute("type", type);
		return "modules/project/fy/batchForm.jsp";
	}

	@RequestMapping("batchUpdate")
	@ResponseBody
	public Map<String, Object> batchUpdate(@RequestParam(required = false)String fyId, @RequestParam(required = false) String type, @RequestParam(required = false)String value) {
		Map<String, Object> result = Maps.newHashMap();
		if(StringUtils.isNotBlank(fyId)) {
			String[] ids = fyId.split(",");
			List<Fy> fyList = Lists.newArrayList();
			for(String id : ids) {
				Fy fytmp = fyForUpdate(id,value,type);
				if(fytmp!=null)fyList.add(fytmp);
			}
			int resultCount = fyService.batchUpdate(fyList);
			result.put("status", "success");
			result.put("message", "更新附页数据成功");
		}else {
			result.put("status", "fail");
			result.put("message", "提交数据有误");
		}
		return result;
	}

	private Fy fyForUpdate(String id,String value, String type) {
		Fy result = new Fy(id);
		result.setUpdateDate(new Date());
		result.setUpdateBy(UserUtils.getUser());
		switch (type) {
			case "qxxz":
				result.setQxxz(value);
				break;
			case "qxdl":
				result.setQxdl(value);
				break;
			case "pdjg":
				result.setPdjg(value);
				break;
			default:
				result = null;
				break;
		}
		return result;
	}

}
